/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.*;

/**
 *
 * @author Minli2020
 */
public class jfSetform extends javax.swing.JFrame {

    private String localFile="";

    /**
     * Creates new form jfSetform
     */
    public jfSetform() {
        initComponents();
        setLocationRelativeTo(null);
        readSetInfo();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jcbExpRoom = new javax.swing.JComboBox<>();
        jLabel3 = new javax.swing.JLabel();
        jtComputer = new javax.swing.JTextField();
        jbUpdate = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jtNumber = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jtName = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jcbDepartment = new javax.swing.JComboBox<>();
        jLabel7 = new javax.swing.JLabel();
        jcbClass = new javax.swing.JComboBox<>();
        jbSave = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jtPassword = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("实验室设置主参数");
        setAlwaysOnTop(true);
        setAutoRequestFocus(false);
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 0, 153));
        jLabel1.setText("设置主参数");

        jLabel2.setText("机房：");

        jcbExpRoom.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "5-104（信息系统实验室）", "5-212（网络空间安全实验室）", "5-213（网络工程实验室）" }));
        jcbExpRoom.setEnabled(false);

        jLabel3.setText("机号：");

        jtComputer.setEnabled(false);

        jbUpdate.setText("修改机位");

        jLabel4.setText("学号：");

        jtNumber.setToolTipText("");

        jLabel5.setText("姓名：");

        jLabel6.setText("学院：");

        jcbDepartment.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "计算机科学与工程学院", "网络与信息中心" }));

        jLabel7.setText("班级：");

        jcbClass.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "21计算机1班", "21计算机2班" }));

        jbSave.setFont(new java.awt.Font("宋体", 1, 15)); // NOI18N
        jbSave.setForeground(new java.awt.Color(0, 0, 153));
        jbSave.setText("保存");
        jbSave.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jbSaveMouseClicked(evt);
            }
        });

        jLabel8.setText("服务器IP和端口：");

        jTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTextField1.setText("10.50.193.61:86");
        jTextField1.setEnabled(false);

        jtPassword.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jtPassword.setText("输入密码");
        jtPassword.setToolTipText("");
        jtPassword.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jcbClass, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jcbDepartment, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtNumber))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jtName))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jtComputer, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jbUpdate)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbSave, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jcbExpRoom, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jLabel1)
                        .addGap(24, 24, 24)
                        .addComponent(jtPassword))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jcbExpRoom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jtComputer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jbUpdate))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(jtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jbSave, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jtNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jcbDepartment, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jcbClass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbSaveMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbSaveMouseClicked
        // TODO add your handling code here:
        String content="";
        if(chkPwd(this.jtPassword.getText())){
            setManCtrl(false);
            content=this.jtIpAddress.getText();
            content+="#"+(String)this.jcbExpRoom.getSelectedItem();
            content+="#"+this.jtComputer.getText();
            saveSetInfo(content,1);
        }
        content=this.jtName.getText();
        content+="#"+this.jtNumber.getText();
        content+="#"+(String)this.jcbDepartment.getSelectedItem();
        content+="#"+(String)this.jcbClass.getSelectedItem();

        saveSetInfo(content,2);
    }//GEN-LAST:event_jbSaveMouseClicked

    private void jbUpdateMouseClicked(java.awt.event.MouseEvent evt){
        setManCtrl(true);
    }

    private boolean chkPwd(String pwd){
        return true;
    }

    public void saveSetInfo(String content,int num){
        File file=new File(localFile);
        BufferedWriter bw=null;
        try{
            if(!file.exists()){
                file.createNewFile();
            }else{
                BufferedReader br=new BufferedReader(new FileReader(file));
                String contentline=br.readLine();
                String curcontent="";
                int inum=1;
                while(contentline!=null){
                    if(curcontent.length()>0){
                        curcontent+="\n";
                    }
                    if(inum==num){
                        curcontent+=content;
                    }else{
                        curcontent+=contentline;
                    }
                    inum++;
                    contentline=br.readLine();
                }

                if(inum==num&&num!=1){
                    content=curcontent+"\n"+content;
                }else if(curcontent.length()>0){
                    content=curcontent;
                }
                br.close();

            }

            bw=new BufferedWriter(new FileWriter(file.getAbsoluteFile(),false));
            bw.write(content);
            bw.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void setManCtrl(boolean state){
        this.jtPassword.setEnabled(state);
        this.jcbExpRoom.setEnabled(state);
        this.jtIpAddress.setEnable(state);
        this.jtComputer.setEnabled(state);
    }

    private void readSetInfo(){
        BufferedReader reader;
        try{
            reader=new BufferedReader(new FileReader(localFile));
            String line=reader.readLine();
            int inums=1;
            while(line!=null){
                showFormInfo(inums,line);
                inums++;
                line=reader.readLine();
            }
            reader.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private  void showFormInfo(int inum,String info){
        String[]items=info.split("#");
        if(inum==1){
            this.jtIpAddress.setText(items[0]);
            this.jcbExpRoom.setSelectedItem(items[1]);
            this.jtComputer.setText(items[2]);
        }else if(inum==2){
            this.jtName.setText(items[0]);
            this.jtNumber.setText(items[1]);
        }
    }







    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(jfSetform.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(jfSetform.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(jfSetform.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(jfSetform.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new jfSetform().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JButton jbSave;
    private javax.swing.JButton jbUpdate;
    private javax.swing.JComboBox<String> jcbClass;
    private javax.swing.JComboBox<String> jcbDepartment;
    private javax.swing.JComboBox<String> jcbExpRoom;
    private javax.swing.JTextField jtComputer;
    private javax.swing.JTextField jtName;
    private javax.swing.JTextField jtNumber;
    private javax.swing.JTextField jtPassword;
    // End of variables declaration//GEN-END:variables
}
